package com.ruoyi.service.impl;

import com.ruoyi.domain.SerivrceInfor;
import com.ruoyi.domain.vo.IndexStatisticsInfor;
import com.ruoyi.mapper.ServceUseingInforMapper;
import com.ruoyi.mapper.ServiceCreateInforMapper;
import com.ruoyi.mapper.ServiceSubInforMapper;
import com.ruoyi.service.IndexStatisticsInforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("IndexStatisticsInforService")
public class IndexStatisticsInforServiceImpl implements IndexStatisticsInforService {
    @Autowired
    ServiceCreateInforMapper serviceCreateInforMapper;
    @Autowired
    ServceUseingInforMapper servceUseingInforMapper;

    @Autowired
    ServiceSubInforMapper serviceSubInforMapper;
    @Override
    public IndexStatisticsInfor getIndexStatisticsInfor() {
        IndexStatisticsInfor indexStatisticsInfor = new IndexStatisticsInfor();
        List<SerivrceInfor> serivrceInforList = serviceCreateInforMapper.getServiceCreateInfor();
        List<SerivrceInfor> useringList = servceUseingInforMapper.getUseingInfor();
        List<SerivrceInfor> serviceSubInforList = serviceSubInforMapper.getServiceSubInfor();
        if(serivrceInforList.size()>0&&useringList.size()>0&&serviceSubInforList.size()>0){
            indexStatisticsInfor.setCreateList(serivrceInforList.stream().map(SerivrceInfor::getTotal).toArray(Integer[]::new));
            indexStatisticsInfor.setUseingList(useringList.stream().map(SerivrceInfor::getTotal).toArray(Integer[]::new));
            indexStatisticsInfor.setSubList(serviceSubInforList.stream().map(SerivrceInfor::getTotal).toArray(Integer[]::new));
        }else {
            return null;
        }
        this.setTrend(indexStatisticsInfor);
        this.settatol(indexStatisticsInfor);
        indexStatisticsInfor.setUseingStatisticsInforList(servceUseingInforMapper.getUseingRankingNow());
        indexStatisticsInfor.setTime(new Date());
        return indexStatisticsInfor;
    }

    public void settatol(IndexStatisticsInfor indexStatisticsInfor){
        int total = 0;
        total=serviceCreateInforMapper.getTotal().getTotal();
        if(total>=1000){
            indexStatisticsInfor.setCteateTotal(String.valueOf(total/1000)+"k");
        }else {
            indexStatisticsInfor.setCteateTotal(String.valueOf(total));
        }
        total=servceUseingInforMapper.getTotal().getTotal();
        if(total>=1000){
            indexStatisticsInfor.setUseingTotal(String.valueOf(total/1000)+"k");
        }else {
            indexStatisticsInfor.setUseingTotal(String.valueOf(total));
        }
        total=serviceSubInforMapper.getTotal().getTotal();
        if(total>=1000){
            indexStatisticsInfor.setSubTotal(String.valueOf(total/1000)+"k");
        }else {
            indexStatisticsInfor.setSubTotal(String.valueOf(total));
        }
    }

    public boolean getis(Integer a){
        return (a&1)==0;
    }

    public void setTrend(IndexStatisticsInfor indexStatisticsInfor){
        Integer[] createList = indexStatisticsInfor.getCreateList();
        Integer[] useingList = indexStatisticsInfor.getUseingList();
        Integer[] subList = indexStatisticsInfor.getSubList();
        int len=createList.length/2;
        int last=createList.length;
        double c1=0;
        double c2=0;
        double u1=0;
        double u2=0;
        double s1=0;
        double s2=0;
        for(int i=0;i<len;i++){
            c1=createList[i]+c1;
            u1=useingList[i]+u1;
            s1=subList[i]+s1;
        }
        for (int i=len;i<last;i++){
            c2=createList[i]+c2;
            u2=useingList[i]+u2;
            s2=subList[i]+s2;
        }
        if (c1==0){
            c1=1;
        }
        if (u1==0){
            u1=1;
        }
        if (s1==0){
            s1=1;
        }
            indexStatisticsInfor.setCreateTrend(Double.parseDouble(String.format("%.2f",((c2-c1)/c1)*100)));
            indexStatisticsInfor.setUseingTrend(Double.parseDouble(String.format("%.2f", ((u2-u1)/u1)*100)));
            indexStatisticsInfor.setSubTrend(Double.parseDouble(String.format("%.2f", ((s2-s1)/s1)*100)));

    }

}
